
propsage.namespace("prestige.receipt");

prestige.receipt.cancel = (function($, ui) {

	function initComponents()
	{
		$('#cancelReceiptDenyDialog').dialog({
			'width': 500,
			'height': 400,
			'autoOpen': false,
			'resizable': false,
			'buttons': {
				'Cancel': {
					'text': 'OK',
					'click': onDialogCancel
				}
			}
		});
	}

	function initEventHandlers()
	{
		$('#btnCancelReceipt').click(onBtnCancelReceiptClick);
	}


	function onDialogCancel() {
		$(this).dialog("close");
		location.reload();
	}
	
	function onBtnCancelReceiptClick() {
		
		if (!$.isEmptyObject(existingPayableItems)) {
			$('#cancelReceiptDenyDialog').dialog("open");
			$("#pv-item-list").empty();
			var pv_list = "<p>";
			var pv_link = "";

			$.each(existingPayableItems, function(i, val) {
				pv_link = baseUrl + '/paymentVoucher/'+ val.paymentVoucherId; 
				pv_list += '\t Payment Voucher No: <a class="blue" href="' + pv_link + '" target="_blank">' + val.paymentVoucherNumber + '</a> <br/>';
			});
			pv_list += '<h4 class="red">Before cancel this receipt, You need to manually cancel all above Payment Vouchers. \n\
					<br/>Notice that all the payable items related to this receipt will also be deleted.</h4></p>';
			
			$(pv_list).appendTo("#pv-item-list");
			return false;
		}
	}

	$(document).ready(function() {
		initComponents();
		initEventHandlers();
	});

}(jQuery, propsage.ui));